#ifndef _DEVICE_MGR_H_
#define _DEVICE_MGR_H_

#include "apdu.h"
#include "../apdu.def/apdu_datatypes.h"

#include "../../devmgr/device_discover.h"

class device_mgr
{
public:
	device_mgr(void);
	~device_mgr(void);

public:

	int init();

	int	get_dev_num();

	int enum_device(char * parameter);

	HANDLE add_exist_device(char * name, HANDLE hCard);

	HANDLE get_first_device();

	HANDLE get_next_device();

	HANDLE get_current_device();

	int get_dev_handle_by_name(char * name, HANDLE * devHandle);

	int get_dev_state( HANDLE dev_handle, enmDevState * state);

	int set_dev_state( HANDLE dev_handle, enmDevState state);

	int get_dev_type(HANDLE dev_handle, long * type);

	char * get_dev_name(HANDLE dev_handle);

	char * get_dev_path(HANDLE dev_handle);

	int set_dev_name(HANDLE dev_handle, char * name);

	int destroy_removed_devices();

	int delete_device(HANDLE dev_handle);

	int open_device(HANDLE dev_handle);

	int close_device(HANDLE dev_handle);

	int open_device_by_name(char * name, HANDLE * dev_handle);

	int transmit_apdu(HANDLE dev_handle,  apdu * pApdu, int * sw );

	int transmit_command( HANDLE dev_handle, unsigned char * cmd, int cmd_len, unsigned char * resp, int * resp_len );

	int send_raw_data(HANDLE dev_handle, unsigned char * cdb, int cdb_len, unsigned char * cmd, long cmd_len);

	int receive_raw_data(HANDLE dev_handle, unsigned char * cdb, int cdb_len, unsigned char * resp, long * resp_len);

	int update_devices_state(int * dev_num, int * removed_num, int * new_num, char * parameter);

	int get_os_handle(HANDLE dev_handle, HANDLE * osHandle);

	int lock_device(HANDLE dev_handle, long timeOut);

	int unlock_device(HANDLE dev_handle);

	int begin_transaction(HANDLE dev_handle);

	int end_transaction(HANDLE dev_handle);

	//high speed device
	int get_hs_apdu_size( HANDLE dev_handle, int *size);

	int enable_high_speed( HANDLE dev_handle, int mode);

	int crypt_with_high_speed( HANDLE dev_handle, unsigned long algo_id, int mode,
		int appid, int containerId, int keyId,
		int firstPackage, unsigned char * iv, int iv_len,
		unsigned char * input, int input_len, unsigned char * output, int *output_len);

private:
	device_base * get_dev_from_handle(HANDLE dev_handle);

	device_base * get_dev_by_name(char * name);

	MK_POSITION   get_pos_from_handle(HANDLE dev_handle);

	int device_io(HANDLE dev_handle, unsigned char* input, int ilen, unsigned char * output, int * olen, int * sw, int timeout);

	int device_present(HANDLE dev_handle);

private:

	device_discover * _dev_discover_ptr;
	
	MK_POSITION _curr_pos;
};

#endif // _DEVICE_H_